<?php require_once 'conn.php';

$type = ( isset($_REQUEST['type'] ) ) ? $_REQUEST['type'] : '';
$num = ( isset($_REQUEST['num'] ) ) ? $_REQUEST['num'] : '';
$mode = ( isset($_REQUEST['mode'] ) ) ? $_REQUEST['mode'] : '';
$access0 = ( isset($_REQUEST['access_0'] ) ) ? $_REQUEST['access_0'] : '';
$access1 = ( isset($_REQUEST['access_1'] ) ) ? $_REQUEST['access_1'] : '';
$access2 = ( isset($_REQUEST['access_2'] ) ) ? $_REQUEST['access_2'] : '';
$access3 = ( isset($_REQUEST['access_3'] ) ) ? $_REQUEST['access_3'] : '';
$access7 = ( isset($_REQUEST['access_7'] ) ) ? $_REQUEST['access_7'] : '';
$access8 = ( isset($_REQUEST['access_8'] ) ) ? $_REQUEST['access_8'] : '';
$access9 = ( isset($_REQUEST['access_9'] ) ) ? $_REQUEST['access_9'] : '';
$access20 = ( isset($_REQUEST['access_20'] ) ) ? $_REQUEST['access_20'] : '';
$access = array(
			'一等奖' => $access1,
			'二等奖' => $access2,
			'三等奖' => $access3,
			//'苏泊尔电磁炉' => $access7,
			//'苏泊尔电饭煲' => $access8,
			//'幸运奖' => $access9,
			'幸运奖' => $access0);
$result = array();
$result['msg'] = "";
switch($type){
	case 'count':
		$count = db_count();
		$result['count'] = $count;
		break;
	case 'user':
		$user = getUser($num);
		$data = "";
		foreach($user as $k => $v){
			$data = ($data == '') ? $v : $data.'|'.$v;
		}
		$result['user'] = $data;
		break;
	case 'add':
		$key = '';
		foreach($access as $item => $list){
			//$parts = explode(',',$list);
			if( count( $list ) > 0 ){
				for($i=0; $i <count($list); $i++){
					addAccess($list[$i],$item);
				}
			}
		}
		$result['msg'] = 'ok';
		break;
	default:
		$result['msg'] = "error";
}
echo json_encode($result);

function addAccess($name,$type){
	global $conn;
	$sql = "INSERT INTO `think_user_access`(`name`,`type`)
    	    VALUES (
      	    	    '".$name."',
       	    	    '".$type."'
			);";
	$res = mysql_query($sql, $conn);
	return $res;
}